<!-- profile.php -->
<?php
session_start();

// 确保用户已登录
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// 数据库连接
$conn = new mysqli("localhost", "root", "", "db");
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 获取用户ID
$user_id = $_SESSION['user_id'];

// 查询用户基本信息
$sql_user = "SELECT * FROM users WHERE id = ?";
$stmt_user = $conn->prepare($sql_user);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$result_user = $stmt_user->get_result();

// 获取用户收到的点赞数
$sql_likes = "SELECT COUNT(*) AS like_count FROM likes WHERE user_id = ?";
$stmt_likes = $conn->prepare($sql_likes);
$stmt_likes->bind_param("i", $user_id);
$stmt_likes->execute();
$result_likes = $stmt_likes->get_result();
$likes_count = $result_likes->fetch_assoc()['like_count'];

// 查询用户发布的动态
$sql_posts = "SELECT * FROM posts WHERE user_id = ? ORDER BY created_at DESC";
$stmt_posts = $conn->prepare($sql_posts);
$stmt_posts->bind_param("i", $user_id);
$stmt_posts->execute();
$result_posts = $stmt_posts->get_result();

$conn->close();

// 用户信息处理
$user_info = $result_user->fetch_assoc();
if (!$user_info) {
    echo "用户信息查询失败或用户不存在";
    exit();
}

// 渲染页面
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars(isset($user_info['username']) ? $user_info['username'] : "未知用户"); ?>的个人主页</title>
    <style>
        body {
            font-family: 'Roboto', sans-serif;
            background-image: linear-gradient(to right, #f4f4f9, #e0e0ea);
            margin: 0;
            padding: 0;
            color: #333;
            transition: background-image 0.5s ease-in-out;
        }

        .container {
            max-width: 800px;
            margin: 40px auto;
            padding: 20px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
            animation: fadeIn 1s ease-in-out forwards;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }

            to {
                opacity: 1;
            }
        }

        h2 {
            color: #4a4a4a;
            text-align: center;
            margin-bottom: 20px;
            animation: slideInFromTop 0.7s ease-in-out both;
        }

        @keyframes slideInFromTop {
            0% {
                transform: translateY(-100%);
            }

            100% {
                transform: translateY(0);
            }
        }

        p {
            margin-bottom: 10px;
        }

        ul {
            list-style-type: none;
            padding: 0;
        }

        li {
            border-bottom: 1px solid #eaeaea;
            padding: 15px 0;
            transition: transform 0.3s ease-in-out;
        }

        li:hover {
            transform: scale(1.05);
        }

        img {
            max-width: 100%;
            height: auto;
            display: block;
            margin-top: 10px;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
    <div>
        <button onclick="location.href='post_submit.php'">发布新动态</button>
    </div>
    <div class="container">
        <h2><?php echo htmlspecialchars(isset($user_info['username']) ? $user_info['username'] : "未知用户"); ?>的个人主页</h2>
        <p>Email: <?php echo htmlspecialchars($user_info['email']); ?></p>
        <p>收到的点赞数: <?php echo $likes_count; ?></p>

        <h3>我的动态</h3>
        <?php if ($result_posts->num_rows > 0) : ?>
            <ul>
                <?php while ($post = $result_posts->fetch_assoc()) : ?>
                    <li id="post-<?php echo $post['id']; ?>">
                        <p><?php echo htmlspecialchars($post['content']); ?></p>
                        <?php if (isset($post['image_url']) && !empty($post['image_url'])) : ?>
                            <img src="<?php echo htmlspecialchars($post['image_url']); ?>" alt="动态图片">
                        <?php endif; ?>
                        <!-- 添加删除按钮 -->
                        <button class="delete-post-btn" data-post-id="<?php echo $post['id']; ?>">删除</button>
                    </li>
                <?php endwhile; ?>
            </ul>
        <?php else : ?>
            <p>暂无动态</p>
        <?php endif; ?>
    </div>
    <script>
document.addEventListener('DOMContentLoaded', function() {
    // 监听所有删除按钮的点击事件
    document.querySelectorAll('.delete-post-btn').forEach(button => {
        button.addEventListener('click', function(event) {
            event.preventDefault(); // 阻止默认行为
            const postId = this.dataset.postId;
            fetch(`delete_post.php?id=${postId}`, { method: 'DELETE' })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 如果删除成功，从页面移除对应的动态
                        document.getElementById(`post-${postId}`).remove();
                    } else {
                        alert('删除动态时出错，请重试');
                    }
                })
                .catch(error => console.error('Error:', error));
        });
    });
});
</script>
</body>

</html>